package com.cuitrip.component.item;

import android.app.Activity;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.res.TypedArray;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.cuitrip.business.tripservice.ui.IconTextView;
import com.cuitrip.component.popupmenu.PopupMenu;
import com.cuitrip.service.R;
import com.lab.rtenvironment.AppCoreRuntime;
import com.lab.utils.ToastUtil;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by sheena on 16/4/20.
 */
public class CombinationItemView extends FrameLayout {

    @Bind(R.id.et_text)
    EditText etText;

    @Bind(R.id.iv_icon)
    IconTextView ivIcon;

    @Bind(R.id.trash_icon)
    IconTextView trashIcon;

    @Bind(R.id.tv_num)
    TextView tvNum;

    private View view;
    private String textHint;
    private String content = "";
    private Context context;
    private ClipboardManager cm;


    public CombinationItemView(Context context) {
        this(context, null);
    }

    public CombinationItemView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CombinationItemView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CombinationItemView, defStyleAttr, 0);
        textHint = a.getString(R.styleable.CombinationItemView_cvTextHint);
        a.recycle();
        init();
    }

    private void init() {
        view = LayoutInflater.from(getContext()).inflate(R.layout.item_comb, this);
        ButterKnife.bind(this);
        cm = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);

        etText.setOnFocusChangeListener(new OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    ivIcon.setVisibility(VISIBLE);
                } else {
                    ivIcon.setVisibility(GONE);
                }
            }
        });

        etText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() > 80) {
                    s.delete(80, s.length());
                }
                tvNum.setText(s.length() + "/" + 80);
            }
        });
        setTextHint(textHint);

    }


    public void setTextHint(String hint) {
        etText.setHint(hint);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getX() >= etText.getRight()) {
            etText.setText("");
            return true;
        }

        if (event.getX() < etText.getLeft()) {
            etText.setText("");
            removeAllViews();
            return true;
        }

        return super.onTouchEvent(event);
    }


    public String getContent() {
        if (!TextUtils.isEmpty(etText.getText())) {
            content = etText.getText().toString();
        }
        return content;
    }

    public void setContent(String content) {
        etText.setText(content);
    }

    public void setTrashIconVisible(boolean isVisible) {
        if (isVisible) {
            trashIcon.setVisibility(VISIBLE);
        } else {
            trashIcon.setVisibility(GONE);
        }
    }

}
